package org.song.videoplayer.floatwindow;

import ohos.agp.components.*;
import ohos.app.Context;

/**
 * Created by song on 2018/1/9.
 * Contact github.com/tohodog
 * 浮窗容器
 * 拦截触摸移动事件，实现浮窗移动
 */

public class FloatMoveView extends StackLayout {

    public FloatMoveView(Context context) {
        this(context, null);
    }

    public FloatMoveView(Context context, AttrSet attrs) {
        this(context, attrs, String.valueOf(0));
    }

    public FloatMoveView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setDraggedListener(Component.DRAG_HORIZONTAL_VERTICAL, new DraggedListener() {
            @Override
            public void onDragDown(Component component, DragInfo dragInfo) {

            }

            @Override
            public void onDragStart(Component component, DragInfo dragInfo) {

            }

            @Override
            public void onDragUpdate(Component component, DragInfo dragInfo) {
                moveListener.move((int) (dragInfo.startPoint.getPointX() + dragInfo.xOffset), (int) (dragInfo.startPoint.getPointY() + dragInfo.yOffset));
            }

            @Override
            public void onDragEnd(Component component, DragInfo dragInfo) {
                moveListener.end();
            }

            @Override
            public void onDragCancel(Component component, DragInfo dragInfo) {
                moveListener.end();
            }
        });
    }

    public interface MoveListener {
        void move(int x, int y);

        void end();
    }

    public void setMoveListener(MoveListener moveListener) {
        this.moveListener = moveListener;
    }

    private MoveListener moveListener;

}
